layui.use(['form', 'layer', 'upload'], function(){
	var form = layui.form,
	layer = layui.layer,
	$ = layui.jquery,
	upload = layui.upload;
	/* 在线加密 */
	upload.render({
		// 绑定元素
		elem: '#SourceGuardian',
		// 上传接口
		url: './ajax.php?act=Upload',
		size: 20480,
		accept: 'file',
		auto: true,
		drag: true,
		exts: 'zip',
		data: {
			// edition: $('input[name="edition"]:checked').val()
			edition: function(){
				return $('input[name="edition"]:checked').val();
			}
		},
		// obj参数包含的信息，跟 choose回调完全一致，可参见上文。
		before: function(obj){
			// 上传loading
			layer.load();
		},
		done: function(data) {
			layer.closeAll('loading');
			if (data.code == 1) {
				$("#space").val(data.data.space);
				$("#count").text(data.data.count);
				$("#money").text(data.data.money);
				$("#fileName").val(data.data.fileName);
				layer.msg(data.msg, {
					icon: 1
				});
			} else {
				layer.msg(data.msg, {
					icon: 5
				});
			}
		},
		error: function(data) {
			//请求异常回调
			layer.confirm("上传失败，您是否要重新上传？", {
				btn: ['确定', '取消'],
				icon: 3,
				title: "提示"
			},
			function() {
				//关闭询问框
				layer.closeAll();
			})
		}
	});
	form.on('radio(edition)', function(data) {
		var edition = data.value;
		// console.log(edition);
		if (edition == 'Sg13') {
			$('#Sg_11').hide();
			$('#Sg_12').hide();
			$('#Sg_13').show();
		} else if (edition == 'Sg12') {
			$('#Sg_11').hide();
			$('#Sg_13').hide();
			$('#Sg_12').show();
		} else if (edition == 'Sg11') {
			$('#Sg_13').hide();
			$('#Sg_12').hide();
			$('#Sg_11').show();
		}
	});
	/* 执行加密 */
	var layer_load;
	form.on('submit(formDemo)',function(data) {
		if ($('#fileName').val() == '') {
			layer.msg('请先上传需要加密的文件！');
			return false;
		}
		var layer_load = layer.msg("客官小二正在努力加密中……", {
			icon: 16,
			time: 0
		});
		//console.log(space);
		var space = $("#space").val();
		var comment = $('input[name="comment"]').val();
		var edition = $('input[name="edition"]:checked').val();
		var arr_box = [];
		$('input[type=checkbox]:checked').each(function() {
			arr_box.push($(this).val());
		});
		$.ajax({
			type: 'POST',
			url: './ajax.php?act=Encode',
			data: {
				'space' : space,
				'edition': edition,
				'version': arr_box,
				'comment': comment
			},
			dataType: 'json',
			success: function(data) {
				if (data.code == 1) {
					var down = layer.confirm(data.msg, {
						btn: ['确定', '取消'],
						closeBtn: 0,
						icon: 1,
						title: '加密完成'
					},
					function() {
						Sg_Down(data.data.down,data.data.name);
						layer.close(down);
					},
					function() {
						layer.msg('感谢您的使用！', {
							icon: 6
						});
					});
				} else {
					layer.msg(data.msg, {
						icon: 2
					});
				}
			},
			error: function(data) {
				//请求异常回调
				layer.confirm("本次操作异常", {
					btn: ['确定', '取消'],
					icon: 3,
					title: "提示"
				},
				function() {
					//关闭询问框
					layer.closeAll();
				})
			}
		});
	});
})

function Sg_Down(space, file) {
	window.location.href = './ajax.php?act=down&space=' + space + '&file=' + file;
}